package com.detrum.sqlite;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.detrum.adapter.ShareListAdatper;
import com.detrum.bean.ShareBean;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by abc on 2018/3/15.
 */

public class SqliteShareOperate {
    private final String TAG="SqliteShareOperate";
    private static SqliteShareOperate sqliteShareOperate;
    private static SQLiteDatabase sqLiteDatabaseWrite,sqLiteDatabaseRead;
    private String table1 = "Share_Info";
    private String table1_key_Name = "name";
    private String table1_key_icon = "icon";
    private String table1_key_packageName = "packageName";
    private String table1_key_activityName = "activityName";
    private String table1_key_select = "isSelect";
    public SqliteShareOperate(Context context) {
        sqLiteDatabaseWrite = SqliteHelper.getInstance(context).getWritableDatabase();
        sqLiteDatabaseRead = SqliteHelper.getInstance(context).getReadableDatabase();
    }

    public static SqliteShareOperate getInstence(Context context){
        if(sqliteShareOperate ==null){
            sqliteShareOperate = new SqliteShareOperate(context);
        }
        synchronized (sqliteShareOperate){
            return sqliteShareOperate;
        }
    }

    public long Insert(ShareBean bean){
        if(bean.getName()==null){
            return -1;
        }
        ContentValues contentValues = new ContentValues();
        contentValues.put(table1_key_Name,bean.getName());
        contentValues.put(table1_key_packageName,bean.getPackageName());
        contentValues.put(table1_key_activityName,bean.getActivityName());
        contentValues.put(table1_key_select,bean.getSelect());
       int count = SelectSingle(bean.getName());
       if(count>0){
           int update = Update(bean);
           return update;
       }
       long insert = sqLiteDatabaseWrite.insert(table1,null,contentValues);
       return insert;
    }

    public int SelectSingle(String name){
        if(name==null){
            return -1;
        }
        Cursor cursor = sqLiteDatabaseRead.query(table1,null,table1_key_Name+" = ?",new String[]{name},null,null,null);
        int count = cursor.getCount();
        return count;
    }

    public int Update(ShareBean bean){
        if(bean==null){
            return -2;
        }
        if(bean.getName()==null){
            return -1;
        }
        String activityName = bean.getActivityName();
        String packageName = bean.getPackageName();
        //下面部分是为了保证，只更新ShareBean对象有的数据，为空跳过。避免更新时将正常数据删除
        ContentValues contentValues = new ContentValues();
        if(activityName!=null){
            contentValues.put(table1_key_activityName,activityName);
        }
        if(packageName!=null){
            contentValues.put(table1_key_packageName,packageName);
        }
        int update = sqLiteDatabaseWrite.update(table1,contentValues,table1_key_Name +" = ?",new String[]{bean.getName()});
        return update;
    }

    public int UpdateSelect(String name,int select){
        ContentValues contentValues = new ContentValues();
        contentValues.put(table1_key_select,select);
        int updateselect = sqLiteDatabaseWrite.update(table1,contentValues,table1_key_Name +" = ?",new String[]{name});
        return updateselect;
    }

    public List<ShareBean> SelectAll(){
        List<ShareBean>list = new ArrayList<>();
        Cursor cursor = sqLiteDatabaseRead.query(table1,null,null,null,null,null,null);
        if(cursor.getCount()==0){
            return null;
        }
        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex(table1_key_Name));
            String packageName = cursor.getString(cursor.getColumnIndex(table1_key_packageName));
            String activityName = cursor.getString(cursor.getColumnIndex(table1_key_activityName));
            int select = cursor.getInt(cursor.getColumnIndex(table1_key_select));
            ShareBean bean = new ShareBean(name,packageName,activityName,select);
            list.add(bean);
        }
        return list;
    }

    public List<ShareBean> SelectAll(int Select){
        List<ShareBean>list = new ArrayList<>();
        Cursor cursor = sqLiteDatabaseRead.query(table1,null,table1_key_select+" = ?",new String[]{Select+""},null,null,null);
        if(cursor.getCount()==0){
            return null;
        }
        while (cursor.moveToNext()){
            String name = cursor.getString(cursor.getColumnIndex(table1_key_Name));
            String packageName = cursor.getString(cursor.getColumnIndex(table1_key_packageName));
            String activityName = cursor.getString(cursor.getColumnIndex(table1_key_activityName));
            int select = cursor.getInt(cursor.getColumnIndex(table1_key_select));
            ShareBean bean = new ShareBean(name,packageName,activityName,select);
            list.add(bean);
        }
        return list;
    }
}
